#include <QWidget>
#include <QSlider>
#include <QVBoxLayout>
#include "ColourFilter.h"
#include <opencv/cv.h>

ColourFilter::ColourFilter() : QObject(),  Filter()
{
	red_min = 0;
	red_max = 255;
	green_min = 0;
	green_max = 255;
	blue_min = 0;
	blue_max = 255;

	initSettings();
}

void ColourFilter::initSettings() {
	settings = new QWidget();

	QSlider *red_min_slider = new QSlider(Qt::Horizontal);
	red_min_slider->setRange(0, 255);
	red_min_slider->setValue(0);
	QSlider *red_max_slider = new QSlider(Qt::Horizontal);
	red_max_slider->setRange(0, 255);
	red_max_slider->setValue(255);
	QSlider *green_min_slider = new QSlider(Qt::Horizontal);
	green_min_slider->setRange(0, 255);
	green_min_slider->setValue(0);
	QSlider *green_max_slider = new QSlider(Qt::Horizontal);
	green_max_slider->setRange(0, 255);
	green_max_slider->setValue(255);
	QSlider *blue_min_slider = new QSlider(Qt::Horizontal);
	blue_min_slider->setRange(0, 255);
	blue_min_slider->setValue(0);
	QSlider *blue_max_slider = new QSlider(Qt::Horizontal);
	blue_max_slider->setRange(0, 255);
	blue_max_slider->setValue(255);
	
	QVBoxLayout *layout = new QVBoxLayout;

	layout->addWidget(red_min_slider);
	layout->addWidget(red_max_slider);
	layout->addWidget(green_min_slider);
	layout->addWidget(green_max_slider);
	layout->addWidget(blue_min_slider);
	layout->addWidget(blue_max_slider);

	settings->setLayout(layout);

	connect(red_min_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedRedMin(int)));
	connect(red_max_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedRedMax(int)));
	connect(green_min_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedGreenMin(int)));
	connect(green_max_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedGreenMax(int)));
	connect(blue_min_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedBlueMin(int)));
	connect(blue_max_slider, SIGNAL(valueChanged(int)), this, SLOT(ChangedBlueMax(int)));
}	

void ColourFilter::processPoint(ImageState *state) {
	IplImage *img = state->frame;
	cvSmooth(img, img);
	uchar *pixel_it = (uchar *) img->imageData;
	uchar *last_pixel = (uchar *) (img->imageData + (img->height-1)*img->widthStep + (img->width-1)*img->nChannels);
	for(; pixel_it <= last_pixel; pixel_it += img->nChannels) 
	{
		if(pixel_it[0] >= blue_min && pixel_it[0] <= blue_max &&
			pixel_it[1] >= green_min && pixel_it[1] <= green_max &&
			pixel_it[2] >= red_min && pixel_it[2] <= red_max)
		{
			pixel_it[0] = 255;
		} else {
			pixel_it[0] = 0;
		}
	}
	IplImage *out_image;
	out_image = cvCreateImage(cvGetSize(img), 8, 1);
	cvSplit(img, out_image, NULL, NULL, NULL);
	cvReleaseImage(&state->frame);
	state->frame = out_image;
	state->image_type = BINARY;
	notifyListener(state);
	
}

void ColourFilter::ChangedRedMin(int newVal) 
{
	red_min = newVal;
}

void ColourFilter::ChangedRedMax(int newVal)
{
	red_max = newVal;
}

void ColourFilter::ChangedGreenMin(int newVal)
{
	green_min = newVal;
}

void ColourFilter::ChangedGreenMax(int newVal)
{
	green_max = newVal;
}

void ColourFilter::ChangedBlueMin(int newVal)
{
	blue_min = newVal;
}

void ColourFilter::ChangedBlueMax(int newVal)
{
	blue_max = newVal;
}

